package actions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import db.dao.Course;
import db.dao.CourseDAO;
import db.dao.TeaCourse;
import db.dao.TeaCourseDAO;
import db.dao.TeaCourseId;
import db.dao.Teacher;
import db.dao.TeacherDAO;
/*添加教师带教课程*/
public class AddTeaCourseAction {
	private String tid2="";	//教师编号
	private String tname="";	//教师姓名
	private String cid2="";	//课程编号
	private String cname="";	//课程名称
	
	public AddTeaCourseAction() {
	}

	public String getTid2() {
		return tid2;
	}

	public void setTid2(String tid2) {
		this.tid2 = tid2;
	}

	public String getTname() {
		return tname;
	}

	public void setTname(String tname) {
		this.tname = tname;
	}

	public String getCid2() {
		return cid2;
	}

	public void setCid2(String cid2) {
		this.cid2 = cid2;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String execute() throws IOException {
		//获取页面信息，用于弹窗提示和清除页面缓存
		HttpServletResponse response = ServletActionContext.getResponse();
		//用于获取结果
		String result="";
		//设置参数
		TeaCourseDAO tcDao = new TeaCourseDAO();
		TeaCourseId teaCourseId = new TeaCourseId();
		teaCourseId.setCourseid(cid2);
		teaCourseId.setTeaid(tid2);
		TeaCourse teaCourse = new TeaCourse();
		teaCourse.setId(teaCourseId);
		//查找课程信息
		CourseDAO cDao = new CourseDAO();
		Course course = cDao.findById(cid2);
		teaCourse.setCourse(course);
		//查找教师信息
		TeacherDAO tDao = new TeacherDAO();
		Teacher teacher = tDao.findById(tid2);
		teaCourse.setTeacher(teacher);
		cname = course.getCoursename();
		tname = teacher.getTeaname();
		teaCourse.setCoursename(cname);
		teaCourse.setTeaname(tname);
		//保存
		result = tcDao.save(teaCourse);
		if (result.equals("success")) {
			//成功弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加成功!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}else {
			//失败弹窗提示
			response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            PrintWriter printWriter = response.getWriter();
            printWriter.print("<script>alert('添加失败!')</script>");
            printWriter.print("<script>window.location.href='/GroupProject/manage.jsp'</script>");
            printWriter.flush();
            printWriter.close();
            response.setHeader("Pragma","No-cache");   
            response.setHeader("Cache-Control","no-cache");  
            response.setHeader("Cache-Control", "no-store");  
            response.setDateHeader("Expires", 0);
			return null;
		}
	}
}
